package com.lenovo.smarttraffic.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GongJiaoBean_7;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Activity_4_4 extends AppCompatActivity {
    private ImageView icon;
    private TextView title;
    private TextView a_4_4_tv1;
    private EditText a_4_4_ed1;
    private EditText a_4_4_ed2;
    private Spinner a_4_4_sp1;
    private Spinner a_4_4_sp2;
    private TextView a_4_4_tv2;
    ArrayList arrayList;
    int p1=0,p2=1;
    List<GongJiaoBean_7.ROWSDETAILBean> beans = new ArrayList<>();
    RequestQueue queue;
    int position = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_4_4);
        initView();
        arrayList = getIntent().getStringArrayListExtra("data");
        a_4_4_tv1.setText(arrayList.get(1)+"-"+arrayList.get(2));
        position = Integer.parseInt(arrayList.get(0).toString());
        getdata();

    }

    private void getdata() {
        JSONObject object = new JSONObject();
        try {
            object.put("UserName", "user1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(JsonRequest.Method.POST, "http://192.168.1.107:8088/transportservice/action/GetBusInfo.do", object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                beans = new Gson().fromJson(jsonObject.toString(), GongJiaoBean_7.class).getROWS_DETAIL();

                ArrayAdapter arrayAdapter = new ArrayAdapter(Activity_4_4.this,R.layout.support_simple_spinner_dropdown_item
                        ,beans.get(position).getSites());
                a_4_4_sp1.setAdapter(arrayAdapter);
                a_4_4_sp2.setAdapter(arrayAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(request);
    }

    private void initView() {
        icon = (ImageView) findViewById(R.id.icon);
        title = (TextView) findViewById(R.id.title);
        a_4_4_tv1 = (TextView) findViewById(R.id.a_4_4_tv1);
        a_4_4_ed1 = (EditText) findViewById(R.id.a_4_4_ed1);
        a_4_4_ed2 = (EditText) findViewById(R.id.a_4_4_ed2);
        a_4_4_sp1 = (Spinner) findViewById(R.id.a_4_4_sp1);
        a_4_4_sp2 = (Spinner) findViewById(R.id.a_4_4_sp2);
        a_4_4_tv2 = (TextView) findViewById(R.id.a_4_4_tv2);
        queue = Volley.newRequestQueue(Activity_4_4.this);
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        title.setText("定制班车");

        a_4_4_sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                p1 = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        a_4_4_sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                p2 = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        a_4_4_tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String ed1 = a_4_4_ed1.getText().toString().trim();
                String ed2 = a_4_4_ed2.getText().toString().trim();
                if (TextUtils.isEmpty(ed1)||TextUtils.isEmpty(ed2)) {
                    Toast.makeText(Activity_4_4.this, "请正确输入", Toast.LENGTH_SHORT).show();
                    return;
                }

                arrayList.add(ed1);
                arrayList.add(ed2);
                arrayList.add(beans.get(position).getSites().get(p1));
                arrayList.add(beans.get(position).getSites().get(p2));

                Log.i("dsadada________size6",arrayList.size()+"");

                Iterator iterator = arrayList.iterator();

                while (iterator.hasNext()){
                    Log.i("dsadada________666",iterator.next()+"");
                }

                startActivity(new Intent(Activity_4_4.this,Activity_4_5.class).putStringArrayListExtra("data",arrayList));

            }
        });
    }
}
